package top.lywivan.mm.service.imp;

import top.lywivan.mm.dao.TagDao;
import top.lywivan.mm.entity.PageResult;
import top.lywivan.mm.entity.QueryPageBean;
import top.lywivan.mm.pojo.Tag;
import top.lywivan.mm.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class TagServiceImpl implements TagService {
    @Autowired
    private TagDao tagDao;

    @Override
    @Transactional(readOnly = true)
    public PageResult findListByPage(QueryPageBean queryPageBean) {
        //System.out.println(queryPageBean.getQueryParams());
        Integer pageSize = queryPageBean.getPageSize();
        Map queryParams = queryPageBean.getQueryParams();
        Tag tag = new Tag();
        if (queryParams.get("name") != null) {
            tag.setName("%" + queryParams.get("name") + "%");
        }
        tag.setStatus((Integer) queryParams.get("status"));
        tag.setCourseId((Integer) queryParams.get("courseId"));
        Long count = tagDao.findTotalCount(tag);
        List<Tag> list = tagDao.findListByPage(queryPageBean.getOffset(), pageSize, tag);
        return new PageResult(count, list);
    }

    @Override
    @Transactional
    public void addTag(Tag tag) {
        tagDao.addTag(tag);
    }

    @Override
    @Transactional
    public void deleteTagById(Integer tagId) {
        tagDao.deleteTagById(tagId);
    }

    @Override
    public List<Tag> findListByCourseId(Integer courseId) {
        return tagDao.findListByCourseId(courseId);
    }
}
